#pragma once

#include "application/map/lift/base_lift.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QElapsedTimer>
#include <QTimer>
#include <QJsonObject>

namespace application {
namespace lift {

class JzLift : public BaseLift {
    Q_OBJECT

public:
    explicit JzLift(int liftNum,
                    const QString& baseUrl,
                    const QString& liftCode,
                    std::shared_ptr<kernel::ILogger> logger = nullptr,
                    QObject* parent = nullptr);
    ~JzLift() override;

    bool initialize() override;
    bool request(const LiftRequestInfo& request) override;
    bool release() override;

    void requestLiftStatus(const QString& uuid, LiftResponseCallback callback) override;
    void requestLiftCall(int liftNum, int callFloor, int aimFloor, LiftResponseCallback callback) override;
    void requestLiftInLiftCloseDoor(int liftNum, LiftResponseCallback callback) override;
    void requestLiftOutLiftCloseDoor(int liftNum, LiftResponseCallback callback) override;
    void requestDoorOpen(const QString& uuid, LiftResponseCallback callback) override;
    void requestDoorClose(const QString& uuid, LiftResponseCallback callback) override;

    int currentFloor() const { return m_currentFloor; }
    int currentDoorState() const { return m_currentDoorState; }

signals:
    void statusUpdated(int floor, int doorState);
    void requestError(const QString& errorMessage);

private slots:
    void onStatusReply();
    void onCallReply();
    void onDoorCmdReply();

private:
    QString buildUrl(const QString& endpoint) const;
    void sendPostRequest(const QString& endpoint,
                         const QJsonObject& payload,
                         LiftResponseCallback callback,
                         void (JzLift::*slot)());
    void handleHttpFailure(QNetworkReply* reply,
                           const QString& context,
                           LiftResponseCallback& targetCallback);

    QNetworkAccessManager* m_networkManager{nullptr};
    QString m_baseUrl;
    QString m_liftCode;
    QString m_dataId;
    QString m_currentDataId;
    LiftResponseCallback m_statusCallback;
    LiftResponseCallback m_commandCallback;
    int m_currentFloor{0};
    int m_currentDoorState{0};
};

} // namespace lift
} // namespace application

